package com.copera.cn.kaka.adapter;

import java.util.List;


import com.copera.cn.kaka.ApplicationCustomer;
import com.copera.cn.kaka.R;
import com.copera.cn.kaka.vo.ExchangeRuleVo;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ExchangeRuleAdapter extends BaseAdapter {
	private LayoutInflater mInflater; 
	private ApplicationCustomer appc;
	private List<ExchangeRuleVo> items;

	private Context context;


	private Handler handler;

	
	public ExchangeRuleAdapter(ApplicationCustomer appc,
			List<ExchangeRuleVo> arrayList, Context context, Handler handler) {
		super();
		this.appc = appc;
		this.items = arrayList;
		this.context = context;
		this.handler = handler;
	}

	@Override
	public int getCount() {
		return items.size();
		}

	@Override
	public Object getItem(int position) {
		return items.get(position);
		}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			mInflater = LayoutInflater.from(context);
			convertView = mInflater.inflate(R.layout.rule_list, null);
			viewHolder.blackline = (RelativeLayout) convertView.findViewById(R.id.blackline);
			viewHolder.delete = (TextView) convertView.findViewById(R.id.delete);
			viewHolder.textView7 = (TextView) convertView.findViewById(R.id.textView7);
			viewHolder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
			viewHolder.textView8 = (TextView) convertView.findViewById(R.id.textView8);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
			viewHolder.blackline = (RelativeLayout) convertView.findViewById(R.id.blackline);
			viewHolder.delete = (TextView) convertView.findViewById(R.id.delete);
			viewHolder.textView7 = (TextView) convertView.findViewById(R.id.textView7);
			viewHolder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
			viewHolder.textView8 = (TextView) convertView.findViewById(R.id.textView8);
			
		}
		final ExchangeRuleVo item = items.get(position);
		viewHolder.textView7.setText(item.getPointCount());
		viewHolder.textView8.setText(item.getExchangeContent());
       viewHolder.delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Message msg = handler.obtainMessage();
				msg.what = 12;
				msg.obj =item.getId(); 
				
				handler.sendMessage(msg);
			}
		});
		return convertView;
	}

	private class ViewHolder {
		RelativeLayout blackline;
		TextView delete;
		TextView textView7;
		TextView textView2;
		TextView textView8;
		
	}

	public void addItem(ExchangeRuleVo exchangeRuleVo) {
		if (exchangeRuleVo != null) {
			items.add(exchangeRuleVo);
		}
	}

	public void clear() {
		items.clear();
	}

}
